package org.jeecg.modules.examcontroller;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.ImportExcelUtil;
import org.jeecg.common.util.SqlInjectionUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.mybatis.MybatisPlusSaasConfig;
import org.jeecg.modules.common.entity.CommonClassify;
import org.jeecg.modules.common.service.ICommonClassifyService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.common.vo.CommonClassifyVO;
import org.jeecg.modules.common.vo.TreeSelectModel;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
* @Description: 分类管理
* @Author: jeecg-boot
* @Date:   2023-08-13
* @Version: V1.0
*/
@Api(tags="分类管理")
@RestController
@RequestMapping("/exam/testQuestions/classify")
@Slf4j
public class TestQuestionsClassifyController extends JeecgController<CommonClassify, ICommonClassifyService> {
   @Autowired
   private ICommonClassifyService commonClassifyService;


   /**
   * 导出excel
   *
   * @param request
   * @param commonClassify
   */
   @RequiresPermissions("common:coll_common_classify:exportXls")
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, CommonClassify commonClassify) {
       commonClassify.setResourcesType(0);
       return super.exportXls(request, commonClassify, CommonClassify.class, "分类管理");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequiresPermissions("common:coll_common_classify:importExcel")
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, CommonClassify.class);
   }


    /**
     * 分类编码0
     */
    private static final String CATEGORY_ROOT_CODE = "0";

    /**
     * 分页列表查询
     * @param sysCategory
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/rootList")
    public Result<IPage<CommonClassify>> queryPageList(CommonClassify sysCategory,
                                                    @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                    @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                    HttpServletRequest req) {

        return commonClassifyService.queryPageList(sysCategory,pageNo,pageSize,req);
    }

    @GetMapping(value = "/childList")
    public Result<List<CommonClassify>> queryPageList(CommonClassify sysCategory,HttpServletRequest req) {

        return commonClassifyService.queryPageList(sysCategory,req);
    }


    /**
     *   添加
     * @param sysCategory
     * @return
     */
    @PostMapping(value = "/add")
    public Result<CommonClassify> add(@RequestBody CommonClassify sysCategory) {

        return commonClassifyService.add(sysCategory);
    }

    /**
     *  编辑
     * @param sysCategory
     * @return
     */
    @RequestMapping(value = "/edit", method = { RequestMethod.PUT,RequestMethod.POST })
    public Result<CommonClassify> edit(@RequestBody CommonClassify sysCategory) {

        return commonClassifyService.edit(sysCategory);
    }

    /**
     *   通过id删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Result<CommonClassify> delete(@RequestParam(name="id",required=true) String id) {


        return commonClassifyService.delete(id);
    }

    /**
     *  批量删除
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    public Result<CommonClassify> deleteBatch(@RequestParam(name="ids",required=true) String ids) {

        return commonClassifyService.deleteBatch(ids);
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<CommonClassify> queryById(@RequestParam(name="id",required=true) String id) {
        return commonClassifyService.queryById(id);
    }




    /**
     * 加载单个数据 用于回显
     */
    @RequestMapping(value = "/loadOne", method = RequestMethod.GET)
    public Result<CommonClassify> loadOne(@RequestParam(name="field") String field,@RequestParam(name="val") String val) {
        return commonClassifyService.loadOne(field,val);
    }

    /**
     * 加载节点的子数据
     */
    @RequestMapping(value = "/loadTreeChildren", method = RequestMethod.GET)
    public Result<List<TreeSelectModel>> loadTreeChildren(@RequestParam(name="pid") String pid) {
       return loadTreeChildren(pid);
    }

    /**
     * 加载一级节点/如果是同步 则所有数据
     */
    @RequestMapping(value = "/loadTreeRoot", method = RequestMethod.GET)
    public Result<List<TreeSelectModel>> loadTreeRoot(@RequestParam(name="async") Boolean async,@RequestParam(name="pid") String pid) {
       return commonClassifyService.loadTreeRoot(async,pid);
    }





    /**
     * 分类字典树控件 加载节点
     * @param pid
     * @param pcode
     * @param condition
     * @return
     */
  /*  @RequestMapping(value = "/loadTreeData", method = RequestMethod.GET)
    public Result<List<TreeSelectModel>> loadDict(@RequestParam(name="pid",required = false) String pid@RequestParam(name="condition",required = false) String condition) {

        return commonClassifyService.loadDict(pid,condition);
    }*/

    /**
     * 分类字典控件数据回显[表单页面]
     *
     * @param ids
     * @param delNotExist 是否移除不存在的项，默认为true，设为false如果某个key不存在数据库中，则直接返回key本身
     * @return
     */
    /*@RequestMapping(value = "/loadDictItem", method = RequestMethod.GET)
    public Result<List<String>> loadDictItem(@RequestParam(name = "ids") String ids, @RequestParam(name = "delNotExist", required = false, defaultValue = "true") boolean delNotExist) {
       return commonClassifyService.loadDictItem(ids,delNotExist);
    }*/

    /**
     * [列表页面]加载分类字典数据 用于值的替换
     * @param code
     * @return
     */
    @RequestMapping(value = "/loadAllData", method = RequestMethod.GET)
    public Result<List<DictModel>> loadAllData(@RequestParam(name="code",required = true) String code) {
        return commonClassifyService.loadAllData(code);
    }

    /**
     * 根据父级id批量查询子节点
     * @param parentIds
     * @return
     */
    @GetMapping("/getChildListBatch")
    public Result getChildListBatch(@RequestParam("parentIds") String parentIds) {
        return commonClassifyService.getChildListBatch(parentIds);
    }

}
